package cn.edu.scau.cmi.wuzhenxin.hibernate.client;

import javafx.fxml.FXML;

import javafx.scene.control.Button;

import javafx.scene.control.TextField;

import java.util.List;

import cn.edu.scau.cmi.wuzhenxin.hibernate.domain.Student;
import cn.edu.scau.cmi.wuzhenxin.hibernate.domain.Teacher;
import cn.edu.scau.cmi.wuzhenxin.hibernate.service.StudentService;
import cn.edu.scau.cmi.wuzhenxin.hibernate.service.TeacherService;
import javafx.event.ActionEvent;

import javafx.scene.control.TextArea;

import javafx.scene.control.ChoiceBox;

public class HibernateController {//核心业务控制类
	@FXML
	private TextArea msgArea;
	@FXML
	private Button btn;
	@FXML
	private Button showbtn;
	@FXML
	private TextField sid;
	@FXML
	private TextField tid;
	@FXML
	private ChoiceBox<String> cmd;
	@FXML
	private TextField sname;
	@FXML
	private TextField tname;

	private StudentService studentService = new StudentService();
	private TeacherService teacherService = new TeacherService(); 
	
	// Event Listener on Button[#btn].onAction
	@FXML
	public void opSet(ActionEvent event) {
		int student_id = 0;
		if(!sid.getText().equals("")) {
			student_id = Integer.parseInt(sid.getText());
		}
		String student_name = sname.getText();
		int teacher_id = 0;
		if(!tid.getText().equals("")) {
			teacher_id = Integer.parseInt(tid.getText());
		}
		String teacher_name = tname.getText();
		String optTypeString = cmd.getValue();
		switch (optTypeString) {
		case "添加学生":
			addStudent(student_name, teacher_id);
			break;
		case "修改学生":
			modifyStudent(student_id, student_name);
			break;
		case "删除学生":
			deleteStudent(student_id, student_name);
			break;
		case "添加老师":
			addTeacher(teacher_name);
			break;
		case "修改老师":
			modifyTeacher(teacher_id, teacher_name);
			break;
		case "删除老师":
			deleteTeacher(teacher_id);
			break;
		case "学生选择指导老师":
			student2Teacher(student_id, teacher_id);
			break;
		case "学生修改指导老师":
			student2Teacher(student_id, teacher_id);
			break;
		case "教师选择被指导的学生":
			student2Teacher(student_id, teacher_id);
			break;
		case "教师修改被指导的学生":
			student2Teacher(student_id, teacher_id);
			break;
		default:
			break;
		}
	}
	// Event Listener on Button[#showbtn].onAction
	@FXML
	public void showOpt(ActionEvent event) {
		List<Student> students = studentService.findAll();
		msgArea.clear();
		for (Student student : students) {
			msgArea.appendText("学生id: "+ student.getId() + "\n");
			msgArea.appendText("学生姓名: "+ student.getName() + "\n");
			msgArea.appendText("指导老师id: "+ student.getTutor().getId() + "\n");
			msgArea.appendText("指导老师姓名: "+ student.getTutor().getName() + "\n");
			msgArea.appendText("-------------------------------------------------\n");
		}
	}
	
	public void addStudent(String name, Integer tid) {
		Teacher tutor = teacherService.findById(tid);
		if(tutor==null) {
			tutor = teacherService.findById(1);
		}
		if(studentService.saveStudent(name, tutor)) {
			msgArea.setText("添加学生成功");
		}else {
			msgArea.setText("添加学生失败");
		}
	}
	
	public void modifyStudent(Integer sid, String name) {
		if(studentService.modifyStudent(sid, name, null)) {
			msgArea.setText("修改学生成功");
		}else {
			msgArea.setText("修改学生失败");
		}
	}
	
	public void deleteStudent(Integer sid, String sname) {
		if(studentService.deleteStudent(sid, sname)){
			msgArea.setText("删除学生成功");
		}else {
			msgArea.setText("删除学生失败");
		}
	}
	
	public void addTeacher(String name) {
		if(teacherService.saveTeacher(name)) {
			msgArea.setText("添加老师成功");
		}else {
			msgArea.setText("添加老师失败");
		}
	}
	
	public void modifyTeacher(Integer id, String name) {
		if(teacherService.modifyTeacher(id, name)) {
			msgArea.setText("修改老师成功");
		}else {
			msgArea.setText("修改老师失败");
		}
	}
	
	public void deleteTeacher(Integer id) {
		if(teacherService.deleteTeacher(id)) {
			msgArea.setText("删除老师成功");
		}else {
			msgArea.setText("删除老师失败");
		}
	}
	
	public void student2Teacher(Integer sid, Integer tid) {
		Teacher tutor = teacherService.findById(tid);
		if(tutor==null) {
			msgArea.setText("老师不存在，修改失败");
			return;
		}
		if(studentService.modifyStudent(sid, "", tutor)) {
			msgArea.setText("修改成功");
		}else {
			msgArea.setText("修改失败");	
		}
	}
}
